<?php
$aOptions = SofaSuppaStore::initOptions( false );
$prod_description_trim = ( strlen( $aOptions[ 'prod_description_trim' ] ) > 0 ) ? stripslashes( $aOptions[ 'prod_description_trim' ] ) : 0;
$prod_use_description = $aOptions[ 'prod_use_description' ];
$entrycurrency = $aOptions[ 'entrycurrency' ];
// layout
$hp_layout = $aOptions[ 'hp_layout' ]; // 'default', 'wide', 'narrow_scrolled', 'narrow_grided'
$hp_num_products = ( strlen( $aOptions[ 'hp_num_products' ] ) > 0 ) ? intval( $aOptions[ 'hp_num_products' ] ) : 6;
$hp_paginas = $aOptions[ 'hp_paginas' ];
$hp_custom_show_title = $aOptions[ 'hp_custom_show_title' ];
// redirect per layout selection
if( $hp_layout != 'default' || $hp_layout != 'none' ) {
	$narrow_grided_page   = get_permalink( intval( get_option( 'welcomenarrowgrid' ) ) );
	$narrow_scrolled_page = get_permalink( intval( get_option( 'welcomenarrow' ) ) );
	$wide_grided_page     = get_permalink( intval( get_option( 'welcomefull' ) ) );
	$common_blog_page     = get_permalink( intval( get_option( 'commonblog' ) ) );
	if( $hp_layout == 'narrow_grided' && $narrow_grided_page ) {
		wp_redirect( $narrow_grided_page );
		die();
	} elseif( $hp_layout == 'narrow_scrolled' && $narrow_scrolled_page ) {
		wp_redirect( $narrow_scrolled_page );
		die();
	} elseif( $hp_layout == 'wide' && $wide_grided_page ) {
		wp_redirect( $wide_grided_page );
		die();
	} elseif( $hp_layout == 'blog' && $common_blog_page ) {
		wp_redirect( $common_blog_page );
		die();
	}
}
?>

<?php get_header(); ?>

<?php if( $hp_layout != 'none' ) { ?>

<!-- main content start -->

	<?php
	// query products - default index page loop
	global $wp_query;
	$pgd = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
	$temp_query = $wp_query;
	$wp_query = NULL;
	$append_query = '';
	if( $hp_paginas == 'yes' ) $append_query = '&paged=' . $pgd;
	$wp_query = new WP_Query( 'post_type=product&post_status=publish&posts_per_page='. $hp_num_products . $append_query );
	?>
    
	<?php if ( have_posts() ) : ?>
    
	<?php
    // different classes are used!
    $switch_class = '';
    $count_rows  = 0;
    ?>

    <div id="featured" class="castshadow grid_8">
    
    	<!-- title start -->
    
		<?php if( $hp_custom_show_title == 'yes' ) { ?>
        <h1><?php _e( "Recently Added to our WebShop", "sofa_suppastore" ); ?></h1>
        <?php } ?>
        
        <!-- title end -->
        
        <!-- wordpress loop start -->
    
    	<?php while ( have_posts() ) : the_post(); ?>
    
    	<?php
		$breakrow = FALSE;
		// switch classes
		if( ( $count_rows + 1 ) % 4 == 0 ) {
			$switch_class = ' omega'; 
			$breakrow = TRUE;
		} elseif( $count_rows % 4 == 0 ) $switch_class = ' alpha';
		else $switch_class = '';
		// get product meta
		$product_meta_price = get_post_meta( get_the_ID(), 'sofa_0_textarea_price', true );
		$product_meta_labeltype = get_post_meta( get_the_ID(), 'labeltype', true );
		$product_meta_labelvalue = get_post_meta( get_the_ID(), 'labelvalue', true );
		?>
    
        <div id="post-<?php the_ID(); ?>" <?php post_class( "grid_2 product$switch_class" ); ?>>
        
            <?php
			// extract image
			if( has_post_thumbnail( get_the_ID() ) ) {
				$default_attr = array( 'class' => "size-thumbnail alignleft", 'alt' => get_the_title(), 'title' => get_the_title() );
				if( $product_meta_labeltype != '' && $product_meta_labelvalue != '' ) echo '<a href="' . get_permalink( get_the_ID() ) . '" rel="' . $product_meta_labeltype . '|' . $product_meta_labelvalue . '">';
				else echo '<a href="' . get_permalink( get_the_ID() ) . '">';
				echo get_the_post_thumbnail( get_the_ID(), 'thumbnail', $default_attr );
				echo '</a>';
			}
			?>
            
        	<span class="prodtitle"><?php the_title(); ?></span>
            
            <div class="prodinfo">
            
            	<span class="prodprice"><?php echo $entrycurrency . ' ' . $product_meta_price; ?></span>
                <span class="addtocart">
                	<a href="<?php echo get_permalink( $post->ID ); ?>" title="<?php _e( "Buy Now", "sofa_suppastore" ); ?>" rel="nofollow"><?php _e( "Buy Now", "sofa_suppastore" ); ?></a>
                </span>
            
            </div>
            
            <?php
			if( $prod_use_description == 'yes' ) {
				$c_post_perma = get_permalink( $post->ID );
				echo '<p>' . trim_the_content( get_the_excerpt(), __( "...READ MORE", "sofa_suppastore" ), $c_post_perma, (int)$prod_description_trim ) . '</p>';
			}
			?>
            
        </div>
        
        <?php 
		if( $breakrow ) echo '<div class="clear">&nbsp;</div>';
		$count_rows++; 
		?>
        
        <?php endwhile; ?>
        
        <!-- wordpress loop end -->
        
        <!-- pagination start -->
        
		<?php
		get_template_part( 'wp-pagenavi' );
		if( function_exists( 'wp_page_numbers' ) && $hp_paginas == 'yes' ) {
			wp_page_numbers();
		}
		elseif( function_exists( 'wp_pagenavi' ) && $hp_paginas == 'yes' ) {
			wp_pagenavi();
		} else {
			global $wp_query;
			$total_pages = $wp_query->max_num_pages;
			if ( $total_pages > 1 && $hp_paginas == 'yes' ) {
				echo '<div id="pagination" class="wp-pagenavi grid_8 alpha omega"><span class="nava">';
					posts_nav_link( ' ', __( '&laquo; Previous page', 'sofa_suppastore' ), __( 'Next page &raquo;', 'sofa_suppastore' ) );
				echo '</span></div>';
			}
		}
        ?>
        
        <!-- pagination end -->
        
    </div>
    
    <?php else: ?>
    
    <!-- no content start -->
    
    <div class="castshadow grid_8">
    
        <div class="grid_8 alpha">
        	<h1><?php _e( "Nothing found!", "sofa_suppastore" ); ?></h1>
            <p><?php _e( "Sorry but no products found. Take your time, there's gonna be soon to see something listed here.", "sofa_suppastore" ); ?></p>
        </div>
        
    </div>
    
    <!-- no content end -->
    
	<?php endif; ?>
    
<!-- main content end -->
    
    <?php
	// backup query aka reset custom query posts loop
	$wp_query = $temp_query;
	$temp_query = NULL;
	?>
    
<!-- include sidebar start -->

    <?php get_template_part( 'sidebar_index' ); ?>
    
<!-- include sidebar end -->

<?php } ?>

<?php get_footer(); ?>

